﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#ifndef STRINGCOMMON_H
#define STRINGCOMMON_H
#include <QColor>
#include <QMutex>
#include <QSerialPort>
#include "sop_errno.h"
#include "QCArray.h"
namespace StringCommonName {
    const QString IDS_STRING_32824 = "\n";
    const QString IDS_STRING_32845 = "Set result ";
    const QString IDS_STRING_32879 = "======================================================";
    const QString IDS_STRING_33091 = "0x";
    const QString IDS_STRING_33109 = "Hardware\\DeviceMap\\SerialComm";
    const QString IDS_STRING_33185 = "COM_INDEX";
    const QString IDS_STRING_33256 = "\\\\.\\COM%1";
    const QString IDS_STRING_33257 = "baud=%d parity=%c data=%d";
    const QString COM_CONFIG = "com_config";
    const QString IP_CONFIG = "ip_config";
    const QString FILE_CONFIG = "file_config";
    const QString INI_FILE_CHECK = "FILE_CHECK";
    const QString INI_FILE_PATH = "FILE_PATH";
    const QString INI_FILE_NAME = "FILE_NAME";
    const QString INI_FILE_INDEX = "FILE_INDEX";
    const QString INI_FILE_SIZE = "FILE_SIZE";
    const QString INI_BURN_ADDR = "BURN_ADDR";
    const QString INI_BURN_SIZE = "BURN_SIZE";
    const QString INI_FILE_TYPE = "FILE_TYPE";
    const QString DLG_CONFIG = "dlg_config";
    const QString SET_CONFIG = "setting_config";
    const QString INI_BAUD = "BAUD";
    const QString INI_DATABIT = "DATABIT";
    const QString INI_STOPBIT = "STOPBIT";
    const QString INI_PARITY = "PARITY";
    const QString INI_FLOWCTRL = "FLOWCTRL";
    const QString INI_COM = "COM";
    const QString INI_IP_INFO = "IP_INFO";
    const QString INI_EFUSE_CONFIG = "EFUSE_CONFIG";
    const QString INI_EFUSE_NAME = "efuse_name";
    const QString INI_EFUSE_START_BIT = "efuse_start_bit";
    const QString INI_EFUSE_BIT_WIDTH = "efuse_bit_width";
    const int TABLEVIEW_COLUMN_CHECK = 0;
    const int TABLEVIEW_COLUMN_NAME = 1;
    const int TABLEVIEW_COLUMN_PAHT = 2;
    const int TABLEVIEW_COLUMN_FILE_INDEX = 3;
    const int TABLEVIEW_COLUMN_FILE_SIZE = 4;
    const int TABLEVIEW_COLUMN_BURN_ADDR = 5;
    const int TABLEVIEW_COLUMN_BURN_SIZE = 6;
    const int TABLEVIEW_COLUMN_TYPE = 7;
    const int TABLEVIEW_COLUMN_ADD = 8;
    const int TABLEVIEW_COLUMN_DELETE = 9;
    const QColor COLOR_RGB_VALUE_WHITE = QColor(255, 255, 255);
    const QColor COLOR_RGB_VALUE_BLACK = QColor(0, 0, 0);
    const QColor COLOR_RGB_VALUE_GREEN = QColor(0, 176, 80);
    const QColor COLOR_RGB_VALUE_ORANGE = QColor(255, 128, 64);
    const QColor COLOR_RGB_VALUE_RED = QColor(255, 0, 0);
    const QColor COLOR_RGB_VALUE_BROWN = QColor(128, 0, 64);
    const QColor COLOR_RGB_VALUE_BLUE = QColor(0, 0, 255);
    const QColor COLOR_RGB_VALUE_YELLOW = QColor(245, 249, 69);
    const QColor COLOR_RGB_VALUE_PINK = QColor(255, 0, 255);
    const int OPEN_SERIAL_PORT_DEFAULT_BAUD_RATE = 115200;
    const int OPEN_SERIAL_PORT_DEFAULT_DATA_BIT = 8;
    const int OPEN_SERIAL_PORT_DEFAULT_STOP_BIT = 0;
    const int OPEN_SERIAL_PORT_DEFAULT_FLOWCTRL = 0;
    const QString CFG_JLINK = "JLink/";
    const QString CFG_JLINK_PATH = "JLinkPath/path";
    const QString CFG_JLINK_DEVICE = CFG_JLINK + "Device";
    const QString CFG_JLINK_IF = CFG_JLINK + "If";
    const QString CFG_JLINK_SPEED = CFG_JLINK + "Speed";
    const char CSP_NAME[30] = "EnterSafe ePass2001 CSP v1.0";
    const uint16_t SEND_BUFFER_SIZE = 64;
    const uint16_t CRC_SIZE = 2;
    const QString MESSAGE_BURN_TOOL = QObject::tr("BurnTool");
    const int QAPP_EXIT_VALUE = 773;
}
using namespace StringCommonName;

namespace common_name {
enum class CRYTO_TYPE {
    CRYTO_TYPE_RSA,
    CRYTO_TYPE_ECC,
};
enum class HI_BURN_FSM {
    FSM_NONE,
    FSM_OPEN_PORT,
    FSM_WAIT_SHAKE_HANDS_ACK,
    FSM_WAIT_FLASH_SHAKE_HANDS_ACK,
    FSM_WAIT_INTERRUPT_FLAG,
    FSM_WAIT_RANDOM,
    FSM_WAIT_SEC_RESULT,
    FSM_WORK,
    FSM_CHECK_IF_CONNECT,
    FSM_BURN_WAIT_START_C,
    FSM_BURN_WAIT_INIT_ACK,
    FSM_BURN_WAIT_INTER_ACK,
    FSM_BURN_WAIT_FINAL_ACK,
    FSM_BURN_WAIT_EOT_C,
    FSM_BURN_WAIT_ZERO_ACK,
    FSM_BURN_WAIT_SELOADR_END_ACK,
    FSM_BURN_UPLOAD_START_C,
    FSM_BURN_UPLOAD_INIT_ACK,
    FSM_BURN_UPLOAD_INTER_ACK, // 需要同时检测是否收到0x04
    FSM_BURN_UPLOAD_EOT_C,
    FSM_BURN_UPLOAD_END_ACK,
    FSM_BURN_WAIT_SELOADR_UPLOAD_ACK,
    FSM_BURN_WAIT_SELOADR_DOWN_ACK,
    FSM_BURN_WAIT_SELOADR_READ_EFUSE_ACK,
    FSM_BURN_WAIT_SELOADR_LOCK_ACK,
    FSM_BURN_WAIT_SELOADR_RESET_ACK,
    FSM_BURN_WAIT_SELOADR_CHANGE_BAUD,
    FSM_BURN_WAIT_MAX,
};

enum BootType {
    BOOT_ROM,
    BOOT_LOADER
};

enum NotifyType {
    NOTIFY_TYPE_ERROR,
    NOTIFY_TYPE_STATE,
    NOTIFY_TYPE_PROGRESS,
    NOTIFY_TYPE_RESULT,
};

enum class BIN_TYPE {
    LOADER,
    NORMAL,
    PARAM,
    EFUSE,
    OTP,
    FLASHBOOT,
};

enum class HSAN_CHIP_TYPE {
    LUOFU = 0x30005,
    XILING = 0x30006,
    EMEI = 0x30007,
    TG0 = 0x30008,
    TG1 = 0x30009,
    TG2 = 0x3000A
};

enum class IMAGE_TYPE {
    IMAGE_TYPE_LOADER,
    IMAGE_TYPE_NORMAL,
    IMAGE_TYPE_KV_NV,
    IMAGE_TYPE_EFUSE,
    IMAGE_TYPE_OTP,
    IMAGE_TYPE_FLASHBOOT,
    IMAGE_TYPE_FACTORY,
    IMAGE_TYPE_VERSION,
    IMAGE_TYPE_SECURITY_A,
    IMAGE_TYPE_SECURITY_B,
    IMAGE_TYPE_SECURITY_C,
    IMAGE_TYPE_PROTOCOL_A,
    IMAGE_TYPE_APPS_A,
    IMAGE_TYPE_RADIOCONFIG,
    IMAGE_TYPE_ROM,
    IMAGE_TYPE_EMMC,
    IMAGE_TYPE_DATABASE = 16, // as database bin 16
    IMAGE_TYPE_FLASHBOOT_3892,
    IMAGE_TYPE_APP_3321,
    IMAGE_TYPE_APP_SIGN_3321,
    IMAGE_TYPE_BT_3321,
    IMAGE_TYPE_BT_SIGN_3321,
    IMAGE_TYPE_DSP_3321,
    IMAGE_TYPE_DSP_SIGN_3321,
    IMAGE_TYPE_SSB_SHA_3321,
    IMAGE_TYPE_SSB_SIGN_3321,
    IMAGE_TYPE_DSP1_3321,
    IMAGE_TYPE_DSP1_SIGN_3321,
    IMAGE_TYPE_SMALL_SSB,
    IMAGE_TYPE_SMALL_SSB_SHA,
    IMAGE_TYPE_SSB_ADD_KEYSTORE,
    IMAGE_TYPE_SSB_ADD_CERTIFICATE,
    IMAGE_TYPE_DFU_BS25,

    IMAGE_TYPE_DDR = 40,
    IMAGE_TYPE_UBOOT_ORIGIN,
    IMAGE_TYPE_ESBC_UART,
    IMAGE_TYPE_ESBC_FASTBOOT,
    IMAGE_TYPE_ALL_FASTBOOT,
    IMAGE_TYPE_ESBC,
    IMAGE_TYPE_UBOOT,
    IMAGE_TYPE_KERNEL,
    IMAGE_TYPE_ROOTFS_RW,
    IMAGE_TYPE_ROOTFS_RO,
    IMAGE_TYPE_FWK_JFFS2,
    IMAGE_TYPE_FWK_SQUASHFS,

    IMAGE_TYPE_FLASH_BIN_3321 = 100,
    IMAGE_TYPE_EMMC_BIN_3321 = 101,
    IMAGE_TYPE_SHA_3321 = 102,
    IMAGE_TYPE_OTP_BIN_3321 = 103
};

struct IMAGE_INFO {
    U8 name[32];    // image name to show, len 32
    U32 startIndex; // image start index in whole file
    U32 imageSize;  // image size
    U32 burnAddr;   // burn addr in flash, like burn 0 200000, equal 0
    U32 burnSize;   // burn size in burn cmd, like burn 0 200000, equal 200000，this size maybe bigger than imageSize
    IMAGE_TYPE type;
};

struct FWPKG_HEAD {
    U32 flag;     // 0xefbeaddf
    U16 crc;      // from imageNum to head end
    U16 imageNum; // image count
    U32 totalFileSize;
    IMAGE_INFO info[0];
};

struct IMAGE_INFO_NEW {
    U8 name[260];   // image name to show, len 32
    U32 startIndex; // image start index in whole file
    U32 imageSize;  // image size
    U32 burnAddr;   // burn addr in flash, like burn 0 200000, equal 0
    U32 burnSize;   // burn size in burn cmd, like burn 0 200000, equal 200000，this size maybe bigger than imageSize
    IMAGE_TYPE type;
};

struct FWPKG_HEAD_NEW {
    U32 flag;
    U16 crc;      // from imageNum to head end
    U16 imageNum; // image count
    U32 totalFileSize;
    U8 name[260]; // image name to show, len 32
    IMAGE_INFO_NEW info[0];
};


struct ENCRYPT_BUFFER {
    DWORD bufferLen;
    BYTE *bufferData;
    ENCRYPT_BUFFER()
        : bufferLen(0),
          bufferData(nullptr) {
    }
    ~ENCRYPT_BUFFER() {
    }
    ENCRYPT_BUFFER(const ENCRYPT_BUFFER &buffer)
    {
        this->bufferLen = buffer.bufferLen;
        this->bufferData = buffer.bufferData;
    }
    ENCRYPT_BUFFER &operator=(const ENCRYPT_BUFFER &buffer)
    {
        this->bufferLen = buffer.bufferLen;
        this->bufferData = buffer.bufferData;
        return *this;
    }
};

struct BIN_INFO {
    QString path;
    QString fileName;
    QString fileIndex;
    QString fileSize;
    QString burnAddr;
    QString burnSize;
    IMAGE_TYPE type;
    BIN_INFO();
    int GetFileIndex() const;
    int GetFileSize() const;
    int GetBurnAddr() const;
    int GetBurnSize() const;
private:
    int Str2U32DecHex(const QString &str) const;
};

struct EraseConfig {
    unsigned int id;
    QString name;
};

using BIN_INFO_ARRAY = QCArray<BIN_INFO, BIN_INFO &>;
const unsigned int TIMER_ID_WAIT_HISILICON = 1;
const unsigned int TIMER_ID_WAIT_RANDOM = 2;
const unsigned int TIMER_ID_WAIT_SEC_RESULT = 3;
const unsigned int TIMER_ID_WAIT_BURN_START_C = 4;
const unsigned int TIMER_ID_WAIT_BURN_INIT_ACK = 5;
const unsigned int TIMER_ID_WAIT_BURN_INTER_ACK = 6;
const unsigned int TIMER_ID_WAIT_BURN_EOT_C = 7;
const unsigned int TIMER_ID_WAIT_ZERO_ACK = 8;
const unsigned int TIMER_ID_WAIT_HIT_AFTER_BURN = 9;
const unsigned int TIMER_ID_SEND = 10;
const unsigned int TIMER_ID_WAIT_NEXT_TIME = 11;
const unsigned int TIMER_ID_UPLOAD_COMMON_TIME = 12;
const unsigned int TIMER_ID_UPLOAD_START_TIME = 13;
const unsigned int TIMER_ID_WAIT_SELOADER_ACK_TIME = 14;
const unsigned int TIMER_ID_COMPLITE_TIME = 15;
const unsigned int TIMER_ID_UPLOAD_RETRY_TIME = 16;
const unsigned int TIMER_ID_CHECK_IF_CONNECT_SEND_TIME = 17;
const unsigned int TIMER_ID_CHECK_IF_CONNECT_TIME = 18;
const unsigned int TIMER_ID_WAIT_SELOADER_END_ACK_TIME = 19;
const unsigned int TIMER_ID_WAIT_INTERRUPT_FLAG = 20;
const unsigned int TIMER_ID_CHECK_COM_TIME = 101;
const int TIMEPERIOD = 2;
const int TIME_RES = 1;
const unsigned int FSM_WAIT_RANDOM_3921_MIN_LEN = 5;
const unsigned int RANDOM_MD_LEN = 32;
const unsigned int MODULUS_DATA_LEN = 300;
const unsigned int EXPONENT_DATA_LEN = 300;
const unsigned int TIMER_ID_WAIT_RANDOM_3921_TIMEOUT = 60000;

const unsigned int MAX_BUFFER_LEN = 409600;
const unsigned int MAX_LOG_BUFFER_LEN = 409600;
const unsigned int SEND_CTRL_PEROID_SEC = 12; // 使用多媒体定时器时,12ms间隔 安全连接基本是可靠的.
const unsigned int SEND_CTRL_PEROID_NONE_SEC = 10; // 12ms也可满足云上打断需求（串口输出的场景）20180720
const unsigned int MAX_SEND_CTRL_CNT = 80;
const unsigned int CTRL_FIXED_LEN = 13;
const unsigned int SHAKE_HAND_SIGNAL_LEN = 18;
const unsigned int FLASH_BOOT_BURN_LEN = 18;

const unsigned int SHA_256_LENGTH = 32;
const unsigned int RSA_2048_LEN = 256;
const unsigned int SIGN_ALG_RSA = 1;
const unsigned int SIGN_ALG_ECC = 2;
const unsigned int ECC_32_BYTES = 32;
const unsigned int ECC_64_BYTES = 64;
const unsigned int ECC_SIGN_MAX_LENGTH = 72;
const unsigned int WM_RCV_COPY_DATA_PACKET = 0x8888;
const unsigned int WM_NOTIFY_STATE = 0x8889;
const unsigned int WM_LOAD_CONFIG = 0x888a;
const unsigned int WM_WIFI_STATE = 0x888b;
const unsigned int WM_SWITCH_DFU = 0x888c;
const unsigned int WM_SWITCH_COM = 0x888d;
const unsigned int EFUSE_ITEM_MAX = 1000;
const unsigned int MAX_RETRY_TIME = 30;
const int BURN_START_ADDRESS_BASE_BIT = 8;
const int MIN_BURN_INTERVAL = 1;
const int MAX_BURN_INTERVAL = 30;

using ERR_BURN_PROCESS = enum {
    ERR_TIMEOUT_WAIT_HISILICON = 1, // 打断超时，可能是选错串口或没复位
    ERR_TIMEOUT_WAIT_RANDOM = 2,
    ERR_TIMEOUT_WAIT_SEC_RESULT = 3,
    ERR_TIMEOUT_WAIT_BURN_START_C = 4,
    ERR_TIMEOUT_WAIT_BURN_INIT_ACK = 5,  // 烧写过程下电或不稳定
    ERR_TIMEOUT_WAIT_BURN_INTER_ACK = 6, // 烧写过程下电或不稳定
    ERR_TIMEOUT_WAIT_BURN_EOT_C = 7,     // 烧写过程下电或不稳定
    ERR_TIMEOUT_WAIT_ZERO_ACK = 8,       // 烧写过程下电或不稳定
    ERR_TIMEOUT_WAIT_HIT_AFTER_BURN = 9,
    ERR_TIMEOUT_SEND = 10,
    ERR_TIMEOUT_WAIT_NEXT_TIME = 11,
    ERR_TIMEOUT_UPLOAD_COMMON_TIME = 12,
    ERR_TIMEOUT_UPLOAD_START_TIME = 13,
    ERR_TIMEOUT_WAIT_SELOADER_ACK_TIME = 14, // 烧写命令下发后没有回复
    ERR_SELOADER_ACK_IS_FAIL = 15,           // 烧写命令下发后返回失败
    ERR_OPEN_FILE = 16,                      // 文件打开失败
    ERR_COM_INVALID = 17,                    // 串口不存在或被占用
    ERR_FILE_NOT_FOUND = 51,
    ERR_FILE_ACCESS_FAIL = 52,
    ERR_USB_IOERR = 74,
    ERR_DEVICE_NOT_CONNECTED = 1167,
    ERR_SEARCH_DEVICE_FAIL = 30000,
    ERR_FAILURE = 0xfffffff0,
    ERR_WAITING = 0xfffffffd,
    ERR_DOING = 0xfffffffe,
};

enum class TimerIdWait {
};

struct BootHeader {
    unsigned int param[6];
    unsigned int subKeyOffset;
};

struct SubKey {
    unsigned int param[11];
    unsigned char encryotFlag;
};

enum class ChipType {
    WIFI5GNB = 3,
    WIFIURANUS,
    SWAN,
    SPARTA,
    WIFIDW31,
    JLINKBS25,
    COMBS25,
    USBBS25,
    MELODY,
    BS2X,
    WS63,
    USBBS2X,
    JLINKBS2X,
    WIFI4GCAT1,
    SW39,
    WS53,
    SW21,
    USB4GCAT1,
    SLEBS2X,
    HSANLUOFU,
    HSANXILING,
    HSANEMEI,
    HSANTIANGONG0,
    HSANTIANGONG1,
    HSANTIANGONG2,
    MCU,
    HI3372,
    HI3322,
    TCPWS63,
    COM2982,
    JLINK2982,
    SLE2982,
    USB2982,
    USBHI3322,
    USBBS27,
    UPGRADE = 99,
    DFU = 100,  // 将dfu放在最后一个
};

enum class WiteThreadMsg {
    WITE_THREAD_MSG_KILLALLTIMER = WM_USER + 0x111,
    WITE_THREAD_MSG_SHOWRESULT,
    WITE_THREAD_MSG_SETRESULT,
    WITE_THREAD_MSG_SETRESULT_A2B,
    WITE_THREAD_MSG_RESETUIANDFSM,
    WITE_THREAD_MSG_INITBURNCMD,
    WITE_THREAD_MSG_STARTCONNECT,
    WITE_THREAD_MSG_RESETRESULTEDIT,
    WITE_THREAD_MSG_SETEDITTEXT,
    WITE_THREAD_MSG_RESETBURNSTATE,
    WITE_THREAD_MSG_CONFIGCOMPORT,
    WITE_THREAD_MSG_WEAKUP,
    WITE_THREAD_MSG_COMMON_ACTION1,
    WITE_THREAD_MSG_COMMON_ACTION2,
    WITE_THREAD_MSG_COMMON_ACTION3,
    WITE_THREAD_MSG_EXIT,
    WITE_THREAD_MSG_CLOSE_JLINK,
    WITE_THREAD_MSG_EXPORT,
    WITE_THREAD_MSG_ERASE_ALL,
    WITE_THREAD_MSG_ERASE,
    WITE_THREAD_MSG_MCU_REOPEN,
    WITE_THREAD_MSG_STARTBURN,
    WITE_THREAD_MSG_GET_PARAMETER,
    WITE_THREAD_MSG_SET_PARAMETER
};
using StateNotifyC = void (*)(NotifyType type, int ret, int pad);

enum class SleThreadMsg {
    SLE_THREAD_MSG_OPEN = 0x00000001,
    SLE_THREAD_MSG_CLOSE,
    SLE_THREAD_MSG_STOP_SEARCH,
    SLE_THREAD_MSG_CONNECT,
    SLE_THREAD_MSG_DISCONNECT,
    SLE_THREAD_MSG_START_BURN,
    SLE_THREAD_MSG_STOP_BURN
};

enum CoreImages {
    CORE_IMAGES_BT = 0,         // BT
    CORE_IMAGES_PROTOCOL = 1,   // DSP
    CORE_IMAGES_APPS = 2,       // APP
    CORE_IMAGES_RECOVERY = 3,
    CORE_IMAGES_EXTERN0 = 4,    // DSP1
    CORE_INAGES_EXTERN1 = 5,
    CORE_IMAGES_EXTERN1_SSB = 6
};

struct ExportInfo {
    U32 addr;
    U32 len;
    QString path;
};

struct HidDeviceInfo {
    uint32_t vid;
    uint32_t pid;
    uint32_t usage;
    uint32_t usagePage;
    QString productName;
    QString serialName;
    QString locationInfo;
    HidDeviceInfo()
        : vid(0),
          pid(0),
          usage(0),
          usagePage(0),
          productName(""),
          serialName(""),
          locationInfo("") {
    }
};

struct ChipTypeInfo {
    uint32_t id;
    uint32_t baudRate;
    QString name;
    QString atReset = "";
};

struct McuConfig {
    QString portName;
    int baudRate;
    int chipModeData;
    int i2cBaudRate;
    int spiBaudRate;
    int canBaudRate;
    int deviceAddress;
    int debugMode;
    int freq;
    McuConfig()
        : portName(""),
          baudRate(0),
          chipModeData(0),
          i2cBaudRate(0),
          spiBaudRate(0),
          canBaudRate(0),
          deviceAddress(0),
          debugMode(0),
          freq(0) {
    }
};

struct IPInfo {
    QString ip;
    QString subnetMask;
    QString gateway;
};

enum class ErrorSettingCom {
    ERROR_SETTING_COM_SUCCESS = 0,
    ERROR_SETTING_COM_SERIAL_PORT_NULL = 1,
    ERROR_SETTING_COM_BAUDRATE = 2,
    ERROR_SETTING_COM_PARITY = 3,
    ERROR_SETTING_COM_FLOW_CTRL = 4,
    ERROR_SETTING_COM_STOPBITS = 5,
    ERROR_SETTING_COM_DATABITS = 6,
    ERROR_SETTING_COM_OTHER = 0xff
};

enum TRANSMISSION_MODE : int {
    UART_MODE,
    UART_USB_MODE,
    JTAG_MODE,
    SWD_MODE,
    SPI_MODE,
    I2C_MODE,
    CAN_MODE
};

enum DEBUG_BOARD : int {
    HISPARK_TRACE,
    HISPARK_LINK
};

enum CHIP_TYPE_MCU : int {
    CHIP_TYPE_AUTO = 0x40000,
    CHIP_TYPE_MCU1 = 0x40001,
    CHIP_TYPE_MCU2 = 0x40002,
    CHIP_TYPE_MCU3 = 0x40003,
    CHIP_TYPE_MCU4 = 0x40004,
    CHIP_TYPE_MCU5 = 0x40005
};

struct McuJtagSwd {
    QString targetFileJTAG;
    QString targetFileSWD;
    QString targetFileInfoJTAG;
    QString targetFileInfoSWD;
    McuJtagSwd()
        : targetFileJTAG(""),
          targetFileSWD(""),
          targetFileInfoJTAG(""),
          targetFileInfoSWD("") {
    }
};

struct McuChipId {
    ulong chipIDAddress;
    ulong chipIDSize;
    McuChipId()
        : chipIDAddress(0),
          chipIDSize(0) {
    }
};

struct MainStartAddrInfo {
    ulong mainStartAddress = 0;
    ulong mainSize = 0;
};

enum class DfuBurnMode {
    DFU = 0,
    AUTO_DFU,
    HID_DFU,
};

struct SramAddrInfo {
    ulong sramStartAddress = 0;
    ulong sramEndAddress = 0;
};

struct ParameterInfo {
    QString name = "";   /* Parameter Name */
    uint digitsNum = 0;  /* Number of bit fields */
    uint newValue = 0;   /* Current Value */
    uint oldValue = 0;  /* Source Value */
    uint defaultValue = -1; /* Default value */
    uint col = 0;
    bool isShow = true;
};

struct ParameterBuff {
    DWORD startAddr;
    QByteArray buff;
    bool lock;
    bool isChange;
};

enum CodeType : int {
    SUCCESS,
    FAIL,
    ACK_TIMEOUT,
    ACK_FAIL,
    FILE_OPEN_FAIL,
    FILE_WRITE_OPEN_FAIL,
    FILE_WRITE_FAIL,
    FILE_READ_FAIL,
    FILE_NULL,
    CMD_NULL,
};

enum class InterfaceParity {
    INTERFACE_PARITY_NO = 0,
    INTERFACE_PARITY_ODD = 1,
    INTERFACE_PARITY_EVEN = 2,
    INTERFACE_PARITY_MARK = 3,
    INTERFACE_PARITY_SPACE = 4
};

enum class STOP_BITS_NUM {
    STOPBITS_VALUE_IS_ONE = 1,
    STOPBITS_VALUE_IS_TWO = 2,
    STOPBITS_VALUE_IS_THREE = 3,
};

struct SleDeviceInfo {
    uint8_t eventType;
    uint8_t addrType;
    unsigned char addr[6] = {0, 0, 0, 0, 0, 0};
    uint8_t directAddrType;
    unsigned char directAddr[6] = {0, 0, 0, 0, 0, 0};
    uint8_t rssi;
    uint8_t dataStatus;
    uint16_t dataLenth;
    uint8_t *data;
    SleDeviceInfo()
        : eventType(0),
          addrType(0),
          directAddrType(0),
          rssi(0),
          dataStatus(0),
          dataLenth(0),
          data(nullptr)
    {
    }
};
}
using namespace common_name;
struct Global {
    static QString dataTime;
    static unsigned char shakeHandSignal[SHAKE_HAND_SIGNAL_LEN];
    static QStringList chipTypeList;
    static ChipType currentChipTypeId;
    static unsigned int chlStartFlag;
    static unsigned int timerPlus;
    static unsigned int timerIdWaitInterruptFlagTimeout;
    static unsigned int timerIdWaitCommonTimeout;
    static unsigned int timerIdWaitLongTimeout;
    static unsigned int timerIdWaitShortTimeout;
    static unsigned int timerIdWaitCommonPrecision;
    static unsigned int timerIdCompliteTimeout;
    static unsigned int timerIdRetryTimeout;
    static unsigned char constShakeHandSignal[SHAKE_HAND_SIGNAL_LEN];
    static QMap <int, ChipTypeInfo> allChipTypeInfo;
    static QMutex mMutex;
    static QSet<QString> mUsbSerialLists;
    static void ReadCfg();
    static void ReadCfgSub();
    static void SaveCfg();
    static QString GetCurrentChipVer();
    static QString GetAppConfigPath();
    static QStringList StringToList(const QString &listStr);
    static QString ListToString(const QStringList &list);
    static QString ChipTypeIdToString(ChipType chipTypeId);
    static ChipType ChipTypeStringToId(const QString &chipType);
    static QString GetAtReset(const ChipType &chipType);
    static void ReadAllChipType();
    static int ReadFileData(const QString &filePath, unsigned char *&data, DWORD& fileSize);
    static void ShowData(const unsigned char* pData, unsigned long ulDataLen);
    static void ActionFailed(DWORD dwLastError);
    static void EndianSwap(BYTE* pData, DWORD length);
    static bool IsUSBType(ChipType type);
    static bool IsHsanType(ChipType type);
    static bool CmdParamWhitelisting(QPair<QString, QString> mapfolder);
    static void CloseApp(unsigned int result);
    static HANDLE OpenDevice(const CHAR *path, BOOL openRw);
    static QString HidEnumerate(QMap<QString, HidDeviceInfo> &mapName);
    static void HidEnumerateAdd(QMap <QString, HidDeviceInfo> &mapName, CHAR* deviceInterfaceList);
    static int Stopbits2Int(int stopBits);
    static int Parity2Int(QSerialPort::Parity par);
    static QByteArray QString2Gbk(QString str);
    static bool IsWs63(ChipType type);
};

#endif // STRINGCOMMON_H
